package com.base.cn.platform.os.service.classes.task;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.classes.task.ClassesTaskDataDao;
import com.base.cn.platform.os.entity.classes.task.ClassesTaskData;
import com.base.cn.platform.os.entity.classes.task.ClassesTaskDataCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 作业数据Biz
 *
 * @author s.li
 * @date 2018/12/24
 */
@Service
public class ClassesTaskDataBiz extends BaseBiz<ClassesTaskData, ClassesTaskDataDao> {

    @Autowired
    private ClassesTaskDataDao classesTaskDataDao;

    /**
     * 保存作业数据
     * @param classesTaskData 作业数据对象
     */
    @Transactional
    public void saveClassesTaskData(ClassesTaskData classesTaskData){
        ClassesTaskData _classesTaskData = this.findOne(" examId = "+classesTaskData.getExamId(),null);
        if(ObjectUtils.isNotEmpty(_classesTaskData)){
            classesTaskData.setCreateTime(_classesTaskData.getCreateTime());
        }
        this.deleteWhereSql(" examId ="+classesTaskData.getExamId());
        List<ClassesTaskData> classesTaskDataList = new ArrayList<>();
        String courseIds = StringUtils.subHeadTailString(classesTaskData.getCourseIds(),",");
        if(StringUtils.isNotEmpty(courseIds)){
            Arrays.stream(courseIds.split(",")).forEach(courseId->{
                if(StringUtils.isNumber(courseId)){
                    ClassesTaskData data = new ClassesTaskData();
                    data.setExamId(classesTaskData.getExamId());
                    data.setStatus(classesTaskData.getStatus());
                    data.setDataId(new BigDecimal(courseId));
                    data.setDataType(1);
                    data.setCreateTime(classesTaskData.getCreateTime());
                    data.setSubjectIds(classesTaskData.getSubjectIds());
                    classesTaskDataList.add(data);
                }
            });
        }
        String classesIds =StringUtils.subHeadTailString(classesTaskData.getClassesIds(),",");
        if(StringUtils.isNotEmpty(classesIds)){
            Arrays.stream(classesIds.split(",")).forEach(classesId ->{
                if(StringUtils.isNumber(classesId)){
                    ClassesTaskData data = new ClassesTaskData();
                    data.setExamId(classesTaskData.getExamId());
                    data.setStatus(classesTaskData.getStatus());
                    data.setDataId(new BigDecimal(classesId));
                    data.setDataType(2);
                    data.setCreateTime(classesTaskData.getCreateTime());
                    data.setSubjectIds(classesTaskData.getSubjectIds());
                    classesTaskDataList.add(data);
                }
            });
        }
        if(ObjectUtils.isNotEmpty(classesTaskDataList)){
            this.batchSave(classesTaskDataList);
        }else{
            classesTaskData.setDataId(BigDecimal.ZERO);
            classesTaskData.setDataType(0);
            this.save(classesTaskData);
        }
    }

    /**
     * 删除作业数据
     * @param examIds 作业ID串
     */
    @Transactional
    public void deleteClassesTaskDataByIds(String examIds){
        this.deleteWhereSql(" examId in ("+examIds+")");
    }

    /**
     * 查询用户作业ID列表
     * @param condition 查询条件
     * @return List<BigDecimal>
     */
    public List<BigDecimal> findUserTaskDataList(ClassesTaskDataCondition condition){
        String userSubjectIds = condition.getSubjectIds();
        boolean notStatus = false;
        if(StringUtils.isNotEmpty(userSubjectIds)){
            String[] arr = userSubjectIds.replaceAll("#","").split(",");
            Set<String> subjectIdSet = new HashSet<>();
            Arrays.stream(arr).forEach(id->{
                if(StringUtils.isNotEmpty(id)){
                    subjectIdSet.add(id);
                }
            });
            if(ObjectUtils.isNotEmpty(subjectIdSet)){
                StringBuilder subjectSql = new StringBuilder("");
                subjectIdSet.forEach(subjectId ->{
                    subjectSql.append(" subjectIds like '%,").append(subjectId).append(",%' or");
                });
                condition.setSubjectSql(subjectSql.substring(0,subjectSql.length()-2));
            }else{
                notStatus = true;
            }
        }else{
            notStatus = true;
        }
        if(notStatus){
            condition.setSubjectSql(" subjectIds ='NOT'");
        }
        List<ClassesTaskData> classesTaskDataList = classesTaskDataDao.findUserTaskDataList(ClassesTaskData.class,condition);
        if(ObjectUtils.isNotEmpty(classesTaskDataList)){
            List<BigDecimal> examIdList = new ArrayList<>();
            classesTaskDataList.forEach(e->{
                examIdList.add(e.getExamId());
            });
            return examIdList;
        }
        return null;
    }
}
